<?php
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;
use think\db\Expression;

class LuckywheelController extends BaseController {
  const AWTYPES = [0 => '普通', 1 => '金币', 2 => '房卡',11=>'改名卡',12=>'记牌器',13=>'双倍经验卡',14=>'3日体验卡',15=>'7日体验卡',16=>'月体验卡'];  // 奖品奖励类型
  /**
   *  @brief  : 后台-左侧菜单
   *  @return : page
   *  @details: LuckyWheel
   */
  public function indexAction() {
    $CHANNELS   = $this->GetAdminChannel();
    $GAME_KIND  = GetGameKind();
    $conds = ['Nullity' => 0];
    $channel = $this->request->param('channel/d', -1);
    if ($channel > 0 && array_key_exists($channel, $CHANNELS)) {
      $conds['Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['Channel'][] = ['EQ', $channel];
    } else {
      $conds['Channel'] = ['IN', array_keys($CHANNELS)];
    }
    $list = table('LuckyGuyConfig', 'rydb.basic')->where($conds)->paginate();
    return $this->fetch('', [
      'list'    => $list,
      'channel' => $channel,
      'AWTYPES' => self::AWTYPES,
      'GAME_KIND' => $GAME_KIND,
      'CHANNELS'  => $CHANNELS,
    ]);
  }
  /**
   *  @brief  : 转盘奖品详情
   *  @return : page|JSON
   *  @details: 
   */
  public function detailAction() {
    $CHANNELS   = $this->GetAdminChannel();
    $GAME_KIND  = GetGameKind();
    $item = [];
    $id = $this->request->param('id/d', 0);
    if ($id > 0) {
      $item = table('LuckyGuyConfig', 'rydb.basic')->where([
        'Channel' => ['IN', array_keys($CHANNELS)],
        'ID' => ['EQ', $id],
      ])->find();
    }
    if ($this->request->isGet()) {
      if (empty($item)) {
        return abort(100, "不存在该转盘设置ID={$id}");
      }
      return $this->fetch('', [
        'item'      => $item,
        'CHANNELS'  => $CHANNELS,
        'GAME_KIND' => $GAME_KIND,
        'AWTYPES'   => self::AWTYPES,
      ]);
    }
    if ($this->request->isPost()) {
      if (empty($item)) {
        return result([], 100, "不存在该转盘设置ID={$id}");
      }
      $data = $this->request->only('Title,Rate,Type,Reward,KindID,Remark');
      //--- 验证器验证 ---
      $retn = $this->validate($data, [ // 按照顺序逐一判断
        'Title|奖品标题'  => 'require|max:100',
        'Rate|中奖概率'   => 'require|integer|between:0,100',
        'Type|奖品类型'   => 'require|in:0,1,2',
        'Reward|奖品价值' => 'require|integer|egt:0',
        'KindID|对应游戏' => 'require|in:0,'.implode(',', array_keys($GAME_KIND)),
        'Remark|备注描述' => 'max:100',
      ]);
      if (true !== $retn) {
        return result([], 101, $retn);
      }
      $data['UpdateTime'] = new Expression('GETDATE()');
      $eax = table('LuckyGuyConfig', 'rydb.basic')->where('ID', $id)->update($data);
      return result([], 0);
    }
  }
}
